package emulator.unittest.hardware.nmos6502.commands;

import org.junit.Before;
import org.junit.Test;

import emulator.EmulatorException;
import emulator.hardware.nmos6502.Flags6502;
import emulator.hardware.nmos6502.commands.SBC;

public class SBCTest  extends CommandTestBase {
	@Before
	public void setUp()	{
		command = new SBC();
	}
	
	@Test
	public void testSubAllZero() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 0, 0, Flags6502.FLAG_CARRY|Flags6502.FLAG_ZERO, 0);
	}

	@Test
	public void testSubWithCarry() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, 0, 0, 0, Flags6502.FLAG_NEGATIVE, 0xff);
	}

	@Test
	public void testSubExample213() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 5, 3, Flags6502.FLAG_CARRY, 2);
	}

	@Test
	public void testSubExample214() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 5, 6, Flags6502.FLAG_NEGATIVE, 255);
	}

	@Test
	public void testSubExample216() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 0, 255, 0, 1);
		executeAndCheckCommandOnAccu(command, 0, 2, 0, Flags6502.FLAG_CARRY, 1);
	}

	@Test
	public void testSubExample217() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 255, 0, Flags6502.FLAG_NEGATIVE|Flags6502.FLAG_CARRY, 255);
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 0, 2, Flags6502.FLAG_NEGATIVE, 254);
	}
	
	@Test
	public void signedOverflowPositive() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 127, 128, Flags6502.FLAG_OVERFLOW|Flags6502.FLAG_NEGATIVE, 255);
	}

	@Test
	public void signedOverflowNegative() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_CARRY, 128, 127, Flags6502.FLAG_OVERFLOW|Flags6502.FLAG_CARRY, 1);
	}

	@Test
	public void testDecimalModeFromZero() throws EmulatorException
	{
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_DECIMAL, 0, 0, Flags6502.FLAG_DECIMAL|Flags6502.FLAG_NEGATIVE, 0x99);
		executeAndCheckCommandOnAccu(command, Flags6502.FLAG_DECIMAL|Flags6502.FLAG_CARRY, 0, 0, Flags6502.FLAG_DECIMAL|Flags6502.FLAG_ZERO|Flags6502.FLAG_CARRY, 0);
	}
}
